<?php
namespace app\admin\controller;
class Refund extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [
                ['and','a.refund_status','>',0],
            ];
            if($d['keyword']) $map[] = ['and','b.username|b.phone','like',"%{$d['keyword']}%"];
            if($d['sh_keyword']) $map[] = ['and','a.sh_name|a.sh_phone|sh_address','like',"%{$d['sh_keyword']}%"];
            if($d['title'])   $map[] = ['and','a.title','like',"%{$d['title']}%"];
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            if($d['refund_status']!='') $map[] = ['and','a.refund_status','=',$d['refund_status']];
            $w  = where($map);
            $sql = "select a.*,group_concat(concat_ws('',title,'(',goods_price_title,')','X',nums) separator '<br/>') as goods_title,b.username from order_goods as a
                          left join user b on a.user_id=b.id $w[0]
                          group by a.order_no order by a.refund_status asc, a.id desc".limit($d['page'],$d['limit']);
            $rs = $this->db->query($sql, $w[1],'',true);

            //下载,圆通模板
            if(isset($d['is_down']) && $d['is_down']){
                $sql = "select a.*,b.username from order_goods as a
                              left join user b on a.user_id=b.id $w[0]
                              order by a.id desc";
                $rs_down = $this->db->query($sql, $w[1]);
                foreach ($rs_down as $k => $v) {
                    $rs_down[$k]['goods_price_title'] = $v['title'] . ' ' . $v['goods_price_title'];
                    $rs_down[$k]['fahuo_name'] = $this->sys['fahuo_name'];
                    $rs_down[$k]['fahuo_phone'] = $this->sys['fahuo_phone'];
                    $rs_down[$k]['fahuo_dizhi'] = $this->sys['fahuo_dizhi'];
                    $rs_down[$k]['fahuo_youbian'] = $this->sys['fahuo_youbian'];
                    $rs_down[$k]['daishouhuokuan'] = '';
                    $rs_down[$k]['shoujianrenyuobian'] = '';
                    $rs_down[$k]['shoujianrenguhua'] = '';
                    $rs_down[$k]['baojiajine'] = '';
                    $rs_down[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                }
                $title = ['物流单号','会员账号','商品名称','数量','单价','总价','收件人姓名','收件人地址','收件人手机号','发件人姓名','发件人电话','发件人地址','发件人邮编','代收货款','备注','收件人邮编','收件人固话','保价金额','日期'];
                $fields= ['kd_no','username','goods_price_title','nums','goods_price_price','total_money','sh_name','sh_address','sh_phone','fahuo_name','fahuo_phone','fahuo_dizhi','fahuo_youbian','daishouhuokuan','kd_note','shoujianrenyuobian','shoujianrenguhua','baojiajine','create_time'];
                $file = com_csv($rs_down,$title,$fields,'ordergoods');
                cookie('js_url',$file);
            }
            if($rs){
              $rule = [
                  'pay_status' => function($v){return c("type.pay_status.{$v}");},
                  'fahuo_status' => function($v){return c("type.fahuo_status.{$v}");},
                  'refund_status' => function($v){
                      if($v == 1){
                          return "<span style='color:red'>" . c("type.refund_status.{$v}") . "</span>";
                      }
                      if($v == 2){
                          return "<span style='color:green'>" . c("type.refund_status.{$v}") . "</span>";
                      }
                  },
                  'create_time' => 'com_time_format',
              ];
              $data = com_format($rs[0],$rule);
              suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //下载订单
    public function downOrder(){
        if(is_post()){//下载
            $d = $this->post;
            $rule = [
                'nos'=>'require',
                'eid'=>'require',
            ];
            $msg = [
                'eid.require' => '请选择快递公司',
            ];
            check($rule,$msg,$d);
            $file = m('OrderGoods','c')->downOrder($d['nos'],$d['eid']);
            cookie('js_url',$file);
            cookie('js_function','preload');
            suc();
        }
        $d = $this->get;
        $orderids = join(',',$d['ids']);
        if(empty($orderids)){
            $orderids =0;
        }
        //查询订单
        $rs = $this->db->query("select * from order_goods where id in ($orderids)",[]);
        $order_nos = join(',',array_column($rs,'order_no'));

        //快递公司列表
        $elist = $this->db->query("select * from express where status = ?",[0]);
        $this->fetch(['nos'=>$order_nos,'elist'=>$elist],'',false);
    }

    public function detail(){
        if(is_post()){
            $id = $this->post('id');
            $type = $this->post('type');
            $info = $this->db->find('order_goods',$id);
            if(!$info){
                return err("订单不存在");
            }
            if($info['refund_status'] != 1){
                return err("该订单不能执行该操作");
            }
            $m = m('orderGoods','c');
            $rs = $m->edit_refund($id,$type,$this->userinfo['id']);
            if($rs){
                cookie('js_msg',"操作成功");
                cookie('js_function','preload');
                suc();
            }else{
                return err($m->getError());
            }
        }
        $id = $this->get("id");
        $sql = "select * from order_goods where id = ? order by id desc";
        $rs = $this->db->query($sql,[$id],1);
        $user = $this->db->query("select * from user where id = ?",[$rs['user_id']],1);
        $this->fetch(["info" => $rs,'user'=>$user]);
    }
}
